package com.hs.sfzy.controller;

import com.github.pagehelper.PageInfo;
import com.hs.sfzy.entity.Free;
import com.hs.sfzy.service.FreeService;
import com.hs.sfzy.service.impl.FreeServiceImpl;
import com.hs.sfzy.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName FreeController
 * @Description 公益中心控制器层
 * @Author shanglin
 * @Date 2024年10月23日 20:55
 * @Version 1.0
 */
@RestController
@RequestMapping("/free")
public class FreeController {
    @Autowired
    private FreeServiceImpl freeService;

    @GetMapping
    public Result selectAll(@RequestBody Free free) {
        List<Free> frees = freeService.selectAll(free);
        return Result.success(frees);
    }

    @GetMapping("/findByPage")
    public Result selectByPage(
            @RequestParam(required = false, defaultValue = "1") int pageNum,
            @RequestParam(required = false, defaultValue = "5") int pageSize,
            Free free) {
        PageInfo<Free> pageInfo = freeService.selectByPageInfo(pageNum, pageSize, free);
        return Result.success(pageInfo);
    }

    @PostMapping("/add")
    public Result insert(@RequestBody Free free) {
        int num = freeService.insert(free);
        if (num != 0) {
            return Result.success("添加成功");
        } else {
            return Result.error("添加失败");
        }
    }

    @PutMapping("/edit")
    public Result update(@RequestBody Free free) {
        int num = freeService.update(free);
        if (num != 0) {
            return Result.success("修改成功");
        } else {
            return Result.error("修改失败");
        }
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") int id) {
        int num = freeService.delete(id);
        if (num != 0) {
            return Result.success("删除成功");
        } else {
            return Result.error("删除失败");
        }
    }
}
